package com.shopping.controller;

import com.alibaba.fastjson.JSONObject;
import com.shopping.constant.ResponseCode;
import com.shopping.pojo.CorporationRole;
import com.shopping.service.corporatonRole.CorporationRoleServiceImpl;
import com.shopping.util.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class CorporationRoleController {

    @Autowired
    CorporationRoleServiceImpl corporationRoleService;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @PostMapping(value = {"/corporationRole/registerCorporation"})
    public Map<String, Object> registerCorporation(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        CorporationRole corporationRole = new CorporationRole();
        corporationRole.setIdNumber(jsonObject.getString("idNumber"));
        corporationRole.setCorporationName(jsonObject.getString("corporationName"));
        corporationRole.setCorporationAddress(jsonObject.getString("corporationAddress"));
        corporationRole.setCorporationIntroduce(jsonObject.getString("corporationIntroduce"));
        Map<String, Object> map = new HashMap<>();
        if(!BeanValidator.validate(corporationRole)){
            map.put("code", ResponseCode.PARAM_ILLEGAL.getValue());
            return map;
        }
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        return corporationRoleService.registerCorporation(corporationRole);
    }

    @PostMapping(value = {"/corporationRole/scanOwnMessage"})
    public Map<String, Object> scanOwnMessage(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        Map<String, Object> map = new HashMap<>();
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        return corporationRoleService.scanOwnMessage(redisUserId);
    }

    @PostMapping(value = {"/corporationRole/showCorporationMessageByName"})
    public Map<String, Object> showCorporationMessageByName(@RequestBody JSONObject jsonObject){
        String corporationName = jsonObject.getString("corporationName");
        return corporationRoleService.showCorporationMessageByName(corporationName);
    }

    @PostMapping(value = {"/corporationRole/showCorporationMessage"})
    public Map<String, Object> showCorporationMessage(@RequestBody JSONObject jsonObject){
        int id = Integer.parseInt(jsonObject.getString("id"));
        return corporationRoleService.showCorporationMessage(id);
    }

    @PostMapping(value = {"/corporationRole/updateMessage"})
    public Map<String, Object> updateMessage(@RequestBody JSONObject jsonObject){
        String uuid = jsonObject.getString("uuid");
        CorporationRole corporationRole = new CorporationRole();
        corporationRole.setIdNumber(jsonObject.getString("idNumber"));
        corporationRole.setCorporationName(jsonObject.getString("corporationName"));
        corporationRole.setCorporationAddress(jsonObject.getString("corporationAddress"));
        corporationRole.setCorporationIntroduce(jsonObject.getString("corporationIntroduce"));
        Map<String, Object> map = new HashMap<>();
        if(!BeanValidator.validate(corporationRole)){
            map.put("code", ResponseCode.PARAM_ILLEGAL.getValue());
            return map;
        }
        int redisUserId = (int) stringRedisTemplate.opsForHash().get(uuid, "userId");
        if(stringRedisTemplate.opsForHash().get(uuid, "openid") == null){
            map.put("code", ResponseCode.SERVER_ERROR.getValue());
            return map;
        }
        return corporationRoleService.updateMessage(corporationRole);
    }

}
